/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.helpers.manto;

import com.uca.sisbec.forms.manto.CarreraForm;
import com.uca.sisbec.mapping.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Susy
 */
public class CarreraHelper {
    
    private Session session;
    
    public CarreraHelper(Session session){
        this.session=session;
    }
    
    public List<Carrera> getAllCarreras(CarreraForm cf){
        List<Carrera> allCarreras;
        String hql = "";
        hql = (cf.getCodUniversidad() != 0) ? "From Carrera where id.idUniv = :univ" : "From Carrera";
        Query query = session.createQuery(hql);
        if (cf.getCodUniversidad() != 0)
            query.setInteger("univ", cf.getCodUniversidad());
        allCarreras = new ArrayList<Carrera>(query.list());
        return allCarreras;
    }
    
    public List<Carrera> getAllCarreras(int iduniv){
        List<Carrera> allCarreras;
        String hql = "";
        hql = (iduniv != 0) ? "From Carrera where id.idUniv = :univ" : "From Carrera";
        Query query = session.createQuery(hql);
        if (iduniv != 0)
            query.setInteger("univ", iduniv);
        allCarreras = new ArrayList<Carrera>(query.list());
        return allCarreras;
    }
    
    
    public List<Carrera> ObtenerTodasCarreras(){
        List<Carrera> allCarreras;
        String hql = "From Carrera";
        Query query = session.createQuery(hql);
        allCarreras = new ArrayList<Carrera>(query.list());
        return allCarreras;
    }
    
    
    public List<Universidad> getUniversidadesActivas(){
        List<Universidad> allUniversidades;
        Query queryResult = session.createQuery("From Universidad where estado='A'");
        allUniversidades = new ArrayList<Universidad>(queryResult.list());
        return allUniversidades;
    }
    
    public void EliminarCarreraDB(int codCarrera){
        Carrera c = (Carrera) session.get(Carrera.class,codCarrera);
        session.delete(c);
    }

    public void InsertarCarreraDB(CarreraForm cf) {
        Carrera car = new Carrera();
        CarreraId id = new CarreraId();
        id.setIdUniv(cf.getCodUniversidad());
        id.setCodCarrera(cf.getCodCarrera());
        car.setId(id);
        car.setCarrera(cf.getNombre());
        car.setEstado(cf.isEstado()?"A":"I");
        session.save(car);
    }
    
    public void obtenerCarreraBecario(CarreraForm cf,Becario bec){
        cf.setCodUniversidad(bec.getUniversidad().getIdUniv());
        cf.setCodCarrera(bec.getCodCarrera());
    }
    
    public boolean validarCarrera(CarreraForm cf,Becario bec){
        CarreraXBecarioId cxbId = new CarreraXBecarioId(cf.getCodCarrera(),cf.getCodUniversidad(),bec.getCarnet());
        CarreraXBecario cxb = (CarreraXBecario) session.get(CarreraXBecario.class, cxbId);
        if(cxb != null){
            System.out.println("Ya existe");
            System.out.println( cxb.getCarrera().getCarrera()+" "+cxb.getId().getIdUniv());
           
            return false;
        }else{
            return true;
        }
    }
    
    public List<CarreraXBecario> obtenerCarrerasBec(Becario bec){
        System.out.println("Entre a obtener Carrera");
        List<CarreraXBecario> cxbList;
        Query queryResult = session.createQuery("From CarreraXBecario where id.carnet = :carnet");
        queryResult.setParameter("carnet", bec.getCarnet());
        cxbList = new ArrayList(queryResult.list());
        return cxbList;
    }
    
    public List<Carrera> obtenerCarreras(Becario bec){
        System.out.println("Entre a obtener Carrera");
        List<Carrera> cxbList;
        Query queryResult = session.createQuery("Select carrera From CarreraXBecario cxb where cxb.id.carnet = :carnet");
        queryResult.setParameter("carnet", bec.getCarnet());
        cxbList = new ArrayList(queryResult.list());
        return cxbList;
    }
    
    public void cambiarBecarioCarrera(CarreraForm cf,Becario bec) throws ParseException{
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(cal.getTime());
        System.out.println("La fecha es: "+date);
        Date fechaIni = sdf.parse(date);
        
        System.out.println("Entre a cambiarBecarioCarrera");
        System.out.println("El becario se llama"+bec.getNombres()+" "+bec.getApellidos());
        CarreraXBecarioId cxbId = new CarreraXBecarioId(bec.getCodCarrera(),bec.getUniversidad().getIdUniv(),bec.getCarnet());
        CarreraXBecario cxb = (CarreraXBecario) session.get(CarreraXBecario.class, cxbId);
        cxb.setEstado("I");
        cxb.setFechaFin(fechaIni);
        System.out.println("A cambiar estado");
        session.update(cxb);
        System.out.println("Lo cambie");
        System.out.println("Logre cambiar la carreraxbecario");
        bec.setCodCarrera(cf.getCodCarrera());
        bec.setUniversidad((Universidad)session.get(Universidad.class,cf.getCodUniversidad()));
        
        session.update(bec);
        CarreraId carId = new CarreraId(cf.getCodCarrera(),cf.getCodUniversidad());
        Carrera car  = (Carrera) session.get(Carrera.class,carId);
        CarreraXBecarioId cxbIdNew = new CarreraXBecarioId(cf.getCodCarrera(),cf.getCodUniversidad(),bec.getCarnet());
        CarreraXBecario cxbNew = new CarreraXBecario();
        cxbNew.setBecario(bec);
        cxbNew.setCarrera(car);
        cxbNew.setEstado("A");
        cxbNew.setId(cxbIdNew);
       
        cxbNew.setFechaInicio(fechaIni);
        session.save(cxbNew);
        
    }

    public void InhabilitarCarreraDB(int cod, int idUniv) {
        CarreraId id = new CarreraId(cod, idUniv);
        Carrera car = (Carrera) session.get(Carrera.class,id);
        car.setEstado("I");
        System.out.println("LLegue aca");
        session.update(car);
    }

    public void HabilitarCarreraDB(int cod, int idUniv) {
        CarreraId id = new CarreraId(cod, idUniv);
        Carrera car = (Carrera) session.get(Carrera.class,id);
        car.setEstado("A");
        session.update(car);
    }
    
}
